package cn.com.dhcc.turbo.web.controller.registry;

import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.modulehandler.valuebean.LikeValue;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.JsonUtil;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.entity.registry.NodePipeline;
import cn.com.dhcc.turbo.service.registry.NodeInfoService;
import cn.com.dhcc.turbo.service.registry.NodePipelineService;
import cn.com.dhcc.turbo.service.registry.vo.PipeLineFtp;
import cn.com.dhcc.turbo.service.registry.vo.PipeLineTlq;

/**
 * 传输通道controller
 * 
 * @日期：2014-5-20 下午2:13:12
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Controller
@RequestMapping("/registry")
public class NodePipelineController extends BaseController {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.SAI);
	@Autowired
	private NodePipelineService nodePipelineService;
	@Autowired
	private NodeInfoService nodeService;
	/**
	 * 传输通道列表
	 * 
	 * @param model
	 * @param pager
	 * @param searchName
	 * @param searchNodeName
	 * @return
	 */
	@RequestMapping(value = "/node_pipeline_list")
	public String list(Model model, Pager pager, String pipeType,String searchNodeName,String islocal) {
		WhereCondition condition = new WhereCondition();
		condition.where();
		if(islocal == null || "".equals(islocal) ){
			islocal = "1";
		}
		condition.eq("t2.islocal", islocal);
		if (!"".equals(pipeType) && pipeType != null) {
			condition.eq("t1.pipetype", pipeType);
		}
		if((!"".equals(pipeType) && pipeType != null) && (!"".equals(searchNodeName) && searchNodeName != null)){
			condition.and();
		}
		if (!"".equals(searchNodeName) && searchNodeName != null) {
			condition.like("t2.node_name", LikeValue.roundLike(searchNodeName));
		}
		
		condition.orderBy("t1.mtime desc");

		try {
			PagerData<NodePipeline> pagerData = nodePipelineService.list(pager,
					condition);
			model.addAttribute(DATA_KEY, pagerData.getDatas());
			setTotoal(model, pagerData.getTotal());
		} catch (Exception e) {
			logger.error("获取传输通道信息列表失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "registry/node_pipeline_list";
	}

	/**
	 * 传输通道添加
	 * 
	 * @param model
	 * @param nodeId
	 * @return
	 */
	@RequestMapping(value = "/node_pipeline_add")
	public String add(Model model, String nodeId) throws DaoException {
		try {
			// NodeInfo nodeInfo = nodeService.getLocalNode();
			// List<NodePipeline> pipeList =
			// nodePipelineService.getNodePipelineByNodeId(nodeInfo.getId());

		} catch (Exception e) {
			logger.error("添加传输通道失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "registry/node_pipeline_add";
	}

	@RequestMapping(value = "/add_node_pipeline", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(Model model, NodePipeline vo,
			String jndiUrl, String ip, Integer port, String dir,
			String username, String password, String remark) {
		try {
			vo.setId(UUIDGenerator.getUUID());
			NodeInfo nodeInfo = nodeService.getLocalNode();
			vo.setNodeId(nodeInfo.getId());
			String paramstr = "";
			if ("0".equals(vo.getPipeType())) {
				PipeLineFtp ftp = new PipeLineFtp();
				ftp.setIp(ip);
				ftp.setPort(port);
				ftp.setRootDir(dir);
				paramstr = JsonUtil.toJson(ftp);
			} else if("2".equals(vo.getPipeType())){
				PipeLineTlq tlq = new PipeLineTlq();
				tlq.setBrokerIp(jndiUrl);
				tlq.setJmsPort(port);
				paramstr = JsonUtil.toJson(tlq);
			}
			vo.setIsEmbed("0");
			vo.setParamStr(paramstr);
			vo.setMtime(new Date());
			nodePipelineService.insert(vo);
			return jsonSuccessResult("添加成功");
		} catch (Exception e) {
			logger.error("添加失败", e);
			return jsonFailResult("添加失败");
		}
	}
	/**
	 * 获取传输通道
	 * @param id
	 * @param model
	 * @return
	 * @throws DaoException
	 */
	@RequestMapping(value = "/node_pipeline_get")
	public String get( Model model,String id) throws DaoException {
		try {
			NodePipeline vo = nodePipelineService.get(id);
			if("0".equals(vo.getPipeType())){
				PipeLineFtp ftp = JsonUtil.toObject(vo.getParamStr(),PipeLineFtp.class);
				model.addAttribute("ip", ftp.getIp());
				model.addAttribute("port", ftp.getPort());
				model.addAttribute("dir", ftp.getRootDir());
				model.addAttribute("pipeTypeText", "FTP");
			}else if("2".equals(vo.getPipeType())){/*
				PipeLineTlq tlq = JsonUtil.toObject(vo.getParamStr(),PipeLineTlq.class);
				model.addAttribute("jndiUrl", tlq.getJndi());
				model.addAttribute("port", tlq.getPort());
				model.addAttribute("pipeTypeText", "TongLINK/Q");
			*/}
			model.addAttribute("pipeType", vo.getPipeType());
			model.addAttribute(DATA_KEY, vo);
		} catch (Exception e) {
			logger.error("获取传输通道失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "registry/node_pipeline_get";
	}
	@RequestMapping(value = "/node_pipeline_update/{id}", method = RequestMethod.GET)
	public String nodePipelineUpdate(@PathVariable String id, Model model) throws DaoException {
		try {
			NodePipeline vo = nodePipelineService.get(id);
			if("0".equals(vo.getPipeType())){
				PipeLineFtp ftp = JsonUtil.toObject(vo.getParamStr(),PipeLineFtp.class);
				model.addAttribute("ip", ftp.getIp());
				model.addAttribute("port", ftp.getPort());
				model.addAttribute("dir", ftp.getRootDir());
				model.addAttribute("pipeTypeText", "FTP");
			}else if("2".equals(vo.getPipeType())){/*
				PipeLineTlq tlq = JsonUtil.toObject(vo.getParamStr(),PipeLineTlq.class);
				model.addAttribute("jndiUrl", tlq.getJndi());
				model.addAttribute("port", tlq.getPort());
				model.addAttribute("pipeTypeText", "TongLINK/Q");
			*/}
			model.addAttribute("pipeType", vo.getPipeType());
			model.addAttribute(DATA_KEY, vo);
		} catch (Exception e) {
			logger.error("获取传输通道失败", e);
			return toAlertPage(model, e.getMessage());
		}
		return "registry/node_pipeline_update";
	}
	/**
	 * 修改传输通道
	 * @param model
	 * @param vo
	 * @param jndiUrl
	 * @param ip
	 * @param port
	 * @param dir
	 * @param username
	 * @param password
	 * @param remark
	 * @return
	 */
	@RequestMapping(value = "/update_node_pipeline", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> updateNodePipeline(Model model, NodePipeline vo,
			String jndiUrl, String ip, Integer port, String dir,
			String username, String password, String remark) {
		try {
			String paramstr = "";
			if ("0".equals(vo.getPipeType())) {
				PipeLineFtp ftp = new PipeLineFtp();
				ftp.setIp(ip);
				ftp.setPort(port);
				ftp.setRootDir(dir);
				paramstr = JsonUtil.toJson(ftp);
			} else if("2".equals(vo.getPipeType())){/*
				PipeLineTlq tlq = new PipeLineTlq();
				tlq.setJndi(jndiUrl);
				tlq.setPort(port);
				paramstr = JsonUtil.toJson(tlq);
			*/}
			vo.setParamStr(paramstr);
			vo.setMtime(new Date());
			nodePipelineService.insertOrUpdateAndSync(vo);
			return jsonSuccessResult("修改成功");
		} catch (Exception e) {
			logger.error("修改失败", e);
			return jsonFailResult("修改失败");
		}
	}
}
